package utils

import com.github.tminglei.slickpg._
import pgSlick.{MyPgHStoreSupport, PgUJsonSupport}
import slick.basic.Capability
import slick.driver.JdbcProfile
import slick.jdbc.JdbcType


trait MyPostgresProfile extends ExPostgresProfile
  with PgArraySupport
  with PgDate2Support
  with PgRangeSupport
  with MyPgHStoreSupport
  with PgUJsonSupport
  with PgSearchSupport
  with PgPostGISSupport
  with PgNetSupport
  with PgLTreeSupport {
  def pgjson = "jsonb"

  override protected def computeCapabilities: Set[Capability] =
    super.computeCapabilities + JdbcProfile.capabilities.insertOrUpdate

  override val api = MyAPI

  object MyAPI extends API with ArrayImplicits
    with DateTimeImplicits
    with JsonImplicits
    with NetImplicits
    with LTreeImplicits
    with RangeImplicits
    with HStoreImplicits
    with SearchImplicits
    with SearchAssistants {
    implicit val strListTypeMapper = new SimpleArrayJdbcType[String]("text").to(_.toList)
    implicit val uJsonArrayTypeMapper =
      new AdvancedArrayJdbcType[ujson.Value](pgjson,
        (s) => utils.SimpleArrayUtils.fromString[ujson.Value](ujson.read(_))(s).orNull,
        (v) => utils.SimpleArrayUtils.mkString[ujson.Value](_.toString())(v)
      ).to(_.toList)

    implicit def mySimpleArrayColumnExtensionMethods[B1, SEQ[B1]](c: Rep[SEQ[B1]])(
      implicit tm: JdbcType[B1], tm1: JdbcType[SEQ[B1]]) = {
      new MyArrayColumnExtensionMethods[B1, SEQ, SEQ[B1]](c)
    }

  }

}

object MyPostgresProfile extends MyPostgresProfile
